package com.qitianyong.decoder;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.SurfaceTexture;
import android.media.MediaCodec;
import android.media.MediaCodecList;
import android.media.MediaFormat;
import android.util.Log;
import android.view.Surface;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;


public class MJpegDecoder {

    private static final String TAG = "MJpegDecoder";
    private final Surface surface;

    private final int width;
    private final int height;
    private int __count = 0;

    private MediaCodec mediaCodec;


    public MJpegDecoder(Surface surface, int width, int height) {
        this.surface = surface;
        this.width = width;
        this.height = height;
//        createDecoder();
    }


    public void createDecoder() {
        try {
            mediaCodec = MediaCodec.createDecoderByType("image/jpeg");
            MediaFormat format = MediaFormat.createVideoFormat("image/jpeg", width, height);
            mediaCodec.configure(format, surface, null, 0);
            mediaCodec.start();
        } catch (Exception e) {
            e.printStackTrace();
            releaseDecoder();
        }
    }

    public void releaseDecoder() {
        if (mediaCodec != null) {
            mediaCodec.stop();
            mediaCodec.release();
            mediaCodec = null;
        }
    }


    public void decodeFrame(byte[] jpegData) {
        try {
            Bitmap bitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
            Canvas canvas = surface.lockCanvas(null);
            if (bitmap != null && canvas != null) {
                Paint paint = new Paint();
                android.graphics.Rect src = new android.graphics.Rect();
                android.graphics.Rect dst = new android.graphics.Rect();
                src.left = 0;
                src.top = 0;
                src.bottom = bitmap.getHeight();
                src.right = bitmap.getWidth();
                dst.left = 0;
                dst.top = 0;
                dst.bottom = height;
                dst.right = width;
                canvas.drawBitmap(bitmap, src, dst, paint);
                Log.d(TAG, "canvas drawImage done " + "src:" + src.bottom + "x" + src.right);
                Log.d(TAG, "canvas drawImage done " + "dst:" + dst.bottom + "x" + dst.right);
                surface.unlockCanvasAndPost(canvas);
            }


//            if (mediaCodec == null) {
//                return;
//            }

//            ByteBuffer[] inputBuffers = mediaCodec.getInputBuffers();
////            ByteBuffer[] outputBuffers = mediaCodec.getOutputBuffers();
//
//            int inputBufferIndex = mediaCodec.dequeueInputBuffer(-1);
//            if (inputBufferIndex >= 0) {
//                ByteBuffer inputBuffer = inputBuffers[inputBufferIndex];
//                inputBuffer.clear();
//                inputBuffer.put(jpegData, 0, jpegData.length);
//                mediaCodec.queueInputBuffer(inputBufferIndex, 0, jpegData.length, 0, 0);
//            }
//
//            MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
//            int outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
//            while (outputBufferIndex >= 0) {
//                mediaCodec.releaseOutputBuffer(outputBufferIndex, true /* render */);
//                outputBufferIndex = mediaCodec.dequeueOutputBuffer(bufferInfo, 0);
//            }
        } catch (
                Exception e) {
            e.printStackTrace();
        }

    }


    public void release() {
        releaseDecoder();
    }
}

